﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;

namespace CodeGenerator.UI.Mybatis
{
    public partial class UcMybatisBasicControl : UserControl
    {
        public UcMybatisBasicControl()
        {
            InitializeComponent();
        }

        public TableInfo tableInfo = new TableInfo();

        private void UcBasicControl_Load(object sender, EventArgs e)
        {
            MainSetting info = Config.GetSetupInfo();

            txtEntityName.Text = info.EntityPackage;
            txtDao.Text = info.DaoPackage;
            txtMapping.Text = info.Mapping;
            txtIservice.Text = info.IServicePackage;
            txtService.Text = info.ServicePackage;

            txtOutputDir.Text = info.OutputDir;
            txtPrefix.Text = info.Prefix;
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowDialog();
            txtOutputDir.Text = folderBrowserDialog.SelectedPath;
        }

        public MybatisCodeCreate GetBasicData()
        {
            MybatisCodeCreate model = new MybatisCodeCreate();

            model.EntityPackage = txtEntityName.Text.Trim();
            model.DaoPackage = txtDao.Text.Trim();
            model.Mapping = txtMapping.Text.Trim();
            model.IServicePackage = txtIservice.Text.Trim();
            model.ServicePackage = txtService.Text.Trim();

            return model;
        }

        /// <summary>
        /// 输出目录
        /// </summary>
        public string OutputDir
        {
            get { return txtOutputDir.Text.Trim(); }
        }

        /// <summary>
        /// 预览Or保存
        /// </summary>
        public bool Preview { 
            get { return chkPreview.Checked; } 
        }

        /// <summary>
        /// 过滤表名
        /// </summary>
        public string Prefix
        { 
            get { return txtPrefix.Text.Trim(); } 
        }

        public bool Validate()
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txtEntityName.Text.Trim()))
            {
                errorProvider1.SetError(txtEntityName, "实体包名不为空！");
                return false;
            }
            if (string.IsNullOrEmpty(txtDao.Text.Trim()))
            {
                errorProvider1.SetError(txtDao, "Dao包名不为空！");
                return false;
            }
            if (string.IsNullOrEmpty(txtMapping.Text.Trim()))
            {
                errorProvider1.SetError(txtDao, "Mapping不为空！");
                return false;
            }
            if (string.IsNullOrEmpty(txtIservice.Text.Trim()))
            {
                errorProvider1.SetError(txtIservice, "Iservice包名不为空！");
                return false;
            }
            if (string.IsNullOrEmpty(txtService.Text.Trim()))
            {
                errorProvider1.SetError(txtService, "Service包名不为空！");
                return false;
            }
            if (string.IsNullOrEmpty(txtOutputDir.Text.Trim()))
            {
                errorProvider1.SetError(txtOutputDir, "输出目录不为空！");
                return false;
            }

            return true;
        }
    }
}
